package com.selfdriving.activity;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.selfdriving.BaseFragmentActivity;
import com.selfdriving.R;
import com.selfdriving.utils.Constant;

public class VerifyActivity extends BaseFragmentActivity implements
		OnClickListener
{

	private int type;
	private ImageView goBreak;
	private TextView topText;
	EditText editText1;
	EditText editText2;
	Button button1;
	Button button2;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.verify);
		type = getIntent().getIntExtra("type", 0);
		initViews();
		initEvents();
	}

	public void btnRegister()
	{
		if (type == 0)
		{
			this.finish();
		} else
		{
			Intent intent = new Intent(this, HomePageActivity.class);
			startActivity(intent);
		}
	}

	@Override
	protected void initViews()
	{
		// TODO Auto-generated method stub
		button1 = (Button) findViewById(R.id.btn_back);
		button2 = (Button) findViewById(R.id.btn_ok);
		editText1 = (EditText) findViewById(R.id.verify_shenfenzheng);
		editText2 = (EditText) findViewById(R.id.verify_carCode);
		goBreak = (ImageView) findViewById(R.id.head_break);
		topText = (TextView) findViewById(R.id.top_text_middle);
		topText.setText("通行证验证");
		goBreak.setVisibility(View.VISIBLE);
	}

	@Override
	protected void initEvents()
	{
		goBreak.setOnClickListener(this);
		button1.setOnClickListener(this);
		button2.setOnClickListener(this);
	}

	@Override
	public void onClick(View v)
	{
		// TODO Auto-generated method stub
		switch (v.getId())
		{
		case R.id.head_break:
			finish();
			break;
		case R.id.btn_back:
			startActivity(ActivityVerifyState.class);
			break;
		case R.id.btn_ok:
			try
			{
				Post();
			} catch (Exception e)
			{
				e.printStackTrace();
			}
			break;
		}
	}

	public void Post()
	{
		String idCard = editText1.getText().toString();
		String carCards = editText2.getText().toString();
		if (idCard.length() < 1)
		{
			showShortToast("请输入您的身份证号码");
			return;
		}
		if (carCards.length() < 1)
		{
			showShortToast("请输入您的车牌号码");
			return;
		}
		AjaxParams params = new AjaxParams();
		params.put("f", "68");
		params.put("phone", mApplication.user.getPhone());
		params.put("idCard", idCard);
		params.put("carCards", carCards);
		showLoadingDialog("通行证验证中...");
		FinalHttp fh = new FinalHttp();
		fh.configTimeout(10000);
		fh.post(Constant.POST, params, new AjaxCallBack<Object>()
		{
			@Override
			public void onFailure(Throwable t, String strMsg)
			{
				showShortToast("验证失败");
				dismissLoadingDialog();
			}

			@Override
			public void onSuccess(Object o)
			{
				String json = o.toString();
				if (json.contains("999999"))
				{
					showShortToast("很抱歉，该车牌号已被验证，如非您本人请进行申述");
				}
				if (json.contains("000000"))
				{
					showShortToast("验证成功");
					finish();
				}
				dismissLoadingDialog();
			}
		});
	}

	@Override
	public void onBackPressed()
	{
		btnRegister();
	}
}
